const JwtStrategy = require('passport-jwt').Strategy
ExtractJwt = require('passport-jwt').ExtractJwt;
const keys = require('../config/keys')

const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrkey;

const conn = require('../api/db')
const sql = "select * from person_info"
let users = [] 
conn.query(sql,(err,res)=> users = res)
module.exports = passport => {
    passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
        const user = users.filter(item=> item.code==jwt_payload.code)[0]
        if(jwt_payload) return done(null,user)
        return done(null,false)
    }))
}